/*
 *  Copyright 2011 Alexey Andreev.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.xthl.core.api;

import java.util.List;
import java.util.Set;
import org.nop.core.Remote;

/**
 *
 * @author Alexey Andreev
 */
@Remote
public interface UserLogic {
    /**
     * Checks if permission is available for current session.
     * @param name permission name.
     * @return whether the specified permission is available.
     */
    boolean checkPermission(String name);
    
    void requirePermission(String name) throws RestrictedAccessException;
    
    /**
     * Gets current user.
     * @return current user or null if not logged in.
     */
    UserDetails getCurrentUser();
    
    /**
     * Gets user with the specified id.
     * @param user id.
     * @return user with the specified id or null if user does not exist.
     */
    UserDetails getUser(int id);
    
    UserDetails getUserByLogin(String login);
    
    @Requires("user.list")
    List<User> getLastUsers(int limit) throws RestrictedAccessException;
    
    @Requires("user.list")
    List<User> findUsers(String loginPart, int limit) throws RestrictedAccessException;
    
    @Suggests("user.edit")
    String getUserEmail(int id) throws RestrictedAccessException;
    
    /**
     * <p>Sets user private email address. If not current user's id,
     * requires 'user.edit' permission. If user contains system role, 
     * also requires 'user.manage'.</p>
     */
    @Suggests({ "user.edit", "user.manage" })
    void setUserEmail(int id, String email) throws RestrictedAccessException;
    
    /**
     * <p>Sets user password. If not current user's id, requires 'user.edit' permission.</p>
     */
    @Suggests({ "user.edit", "user.manage" })
    void setUserPassword(int userId, String password) throws RestrictedAccessException;
    
    /**
     * <p>Enables or disables user.</p>
     * @param userId user id.
     * @param enabled whether to enable or disable user account
     */
    @Requires("user.enable")
    void enableUsers(Set<Integer> userIds, boolean enabled) throws RestrictedAccessException;
    
    /**
     * <p>Sets user permissions.</p>
     * @param userId user id.
     * @param permissions which permissions to assign to user.
     */
    @Requires("user.manage")
    void setUserPermissions(int userId, Set<String> permissions)
            throws RestrictedAccessException;
    
    @Requires("user.edit")
    void setUserRoles(int userId, Set<Integer> roleIds)
            throws RestrictedAccessException;
    
    @Requires("user.create")
    UserDetails createUser(CreateUserCommand command) throws RestrictedAccessException;
    
    @Requires("user.accept")
    void activateUsers(Set<Integer> userIds) throws RestrictedAccessException;
    
    void activateWithHash(int id, String hash) throws IllegalActivationHashException;
    
    @Requires("user.manage")
    List<String> getUserPermissions(int userId) throws RestrictedAccessException;
    
    @Suggests("user.edit")
    boolean checkPassword(int userId, String password) throws RestrictedAccessException;
}
